<?php
// +----------------------------------------------------------------------+
// | The Answern Inc                                                  |
// +----------------------------------------------------------------------+
// | Copyright (c) 2017, Answern Inc. All rights reserved.            |
// +----------------------------------------------------------------------+
// | Authors: Answern Inc. 
// |                                                                      |
// +----------------------------------------------------------------------+
 
/**
 * @version  1.0
 * @author   zxiaofee
 * @date     2017-05-11
 */

namespace App\Controllers\Common;
use \App\Libs\Base\Constant;
use App\Models\User\User;

class SetBaby extends \App\Libs\Base\BaseAction
{
	 protected function beforeRun() {
         parent::beforeRun();
         $this->needLogin = TRUE;
     }
	public function execute() {
		$user = $this->session->user;
	/*	$user = User::find(array(
			'phone = ?0',
			'bind' => array('15699956172'),
		));*/
		$babyName = $this->request->get('babyName');
		$babySex = empty( $this->request->get('babySex') )?0:$this->request->get('babySex');
		$babyBirthday = $this->request->get('babyBirthday');
		$user->baby_name = $babyName;
		$user->baby_sex = $babySex;
		$user->baby_birthday  = $babyBirthday;
		$result = $user->save();
		if( !$result ){
			//失败
			return \App\Libs\Views\Json::view(1, '失败', null);
		}
		\App\Libs\Session\Session::resetCache($user);
		$user = $user->toArray();
		$user['age'] = \App\Libs\Helper\CommonHelper::age($user['baby_birthday']);
		$user['vip'] = $user['vip'] == 1?'已开通':'未开通';
		$user['name'] = $user['username'];
		return \App\Libs\Views\Json::view(0, '成功', $user);
	}
}
